home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: Comma Delimited function wanted
- Date: 11 Jan 1996 21:21:00 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4d4gic$p7u@umbc9.umbc.edu>
- References: <4d1l42$mb6@voyager.Internex.NET>
- NNTP-Posting-Host: f-umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Ian H. Stewart <ian_stewart@nyro.com> wrote:
- |> Help with a comma delimited function needed.
- |>
- |> I did this once before, but I am blocked and can't
- |> find the original source.
- |>
- |> Here is what I need to do:
- |>
- |> I have a buffer full of text.
- |>
- |> char buffer = "3740067099,914885AC2,P03,5000";
-
- That should be a char *...
-
- |> I have four char vars to put this into.
- |>
- |> char field1[20], field2[20], field3[20], field4[20];
- |>
- |> What I want to do is get this as the end result.
- |>
- |> field1 = 3740067099
- |> field2 = 914885AC2
- |> field3 = P03
- |> field4 = 5000
- |>
- |> Anyway, help is appreciated.
-
- I'd think sscanf() is the easiest solution although strtok() would work
- as well as custom writing a character by character scanning function.
- Here's a little example using sscanf(). I hope I got the scanset right ;-).
-
- #include <stdio.h>
-
- int main (void)
- {
- char *buffer = "3740067099,914885AC2,P03,5000";
- char field1[20],
- field2[20],
- field3[20],
- field4[20];
-
- sscanf (buffer, "%[^,],%[^,],%[^,],%s", field1, field2, field3, field4);
- printf ("field1 = %s\n", field1);
- printf ("field2 = %s\n", field2);
- printf ("field3 = %s\n", field3);
- printf ("field4 = %s\n", field4);
-
- return (0);
- }
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-